import logging
from config import parse
from tool import mns_math
from log import mns_log
from host import host

mns_log.Log.mns_logging()


class Ship:

    def __init__(self, ship_name, pos_x, pos_y, course, speed, host_group):
        self.host_group = host_group
        self.host = None
        self.name = ship_name
        self.px = pos_x
        self.py = pos_y
        self.course = course
        self.speed = speed
        self.max_x, self.max_y = parse.Parse.get_world_size()
        logging.info("新建船只" + self.name + ",船只位置为" + str(self.px) + "," + str(self.py))

    def install_host(self, my_host: host.Host):
        """给船只安装节点"""
        self.host = my_host

    def run_ship(self, time: float):
        """船只运动，并获得船只运动后位置"""
        # 单位时间航行距离
        dis = self.speed * time / 1000
        # 预计运行的位置
        if self.course <= 90:
            c = self.course
            y = 0 - (dis * mns_math.sin(c))
            x = 0 - (dis * mns_math.cos(c))
        elif self.course <= 180:
            c = 180 - self.course
            y = 0 - (dis * mns_math.sin(c))
            x = dis * mns_math.cos(c)
        elif self.course <= 270:
            c = 270 - self.course
            x = dis * mns_math.sin(c)
            y = dis * mns_math.cos(c)
        else:
            c = 360 - self.course
            y = dis * mns_math.sin(c)
            x = 0 - (dis * mns_math.cos(c))
        # 如果下一步船将会走出地图边界则返航，这一步不动
        predict_x, predict_y = self.px + x, self.py + y
        if predict_x < 0 or predict_y < 0 or predict_x > float(self.max_x) or predict_y > float(self.max_y):
            self.course = (self.course + 180) % 360
            # logging.info("船只" + self.name + "折返,当前航向为" + str(self.course))
        else:
            self.px = predict_x
            self.py = predict_y
        # logging.info(
        #     "船" + self.name + "当前位置为:" + str(mns_math.mns_round(self.px)) + "," + str(
        #         mns_math.mns_round(self.py)))
        return self.px, self.py

    def get_current_pos(self):
        """获得船只当前位置"""
        return self.px, self.py

    def get_name(self):
        """获得船只名字"""
        return self.name

    def get_host_group(self):
        """获得船只节点组"""
        return self.host_group


if __name__ == "__main__":
    c = 360
    print(mns_math.sin(c))
    print(mns_math.cos(c))
    ship = Ship(11, 0, 0, c, 100)
    for i in range(3):
        print(ship.run_ship(10))
